/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.QueryException;
import org.hibernate.Session;
import org.hibernate.SessionException;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.exception.JDBCConnectionException;
import util.HibernateUtil;

/**
 *
 * @author Quan
 */
public abstract class TableDAO {

    protected String hql_laydanhsach;
    protected String hql_lay;

    public List LayDanhSach() throws Exception {
        List ds = null;
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        try {
            org.hibernate.Query query = session.createQuery(hql_laydanhsach);
            ds = query.list();
        } catch (SessionException e) {
            throw new QueryException(e.getMessage());
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        } finally {
            session.close();
        }
        return ds;
    }

    public void Them(Object ob) throws Exception {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        
        try {
            session.save(ob);
            tx.commit();
        } catch (JDBCConnectionException e) {
            tx.rollback();
            throw new Exception("Lỗi kết nối");
        } catch (org.hibernate.exception.ConstraintViolationException e) {
            tx.rollback();
            throw new Exception(e.getMessage() + "\nCheck at " + e.getConstraintName());
        } catch (Exception e) {
            tx.rollback();
            throw new Exception(e.getMessage());
        } finally {
            session.close();
        }
    }

    public void Sua(Object ob) throws Exception {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        try {
            session.update(ob);
            tx.commit();
        } catch (JDBCConnectionException e) {
            tx.rollback();
            throw new Exception("Lỗi kết nối");
        } catch (org.hibernate.exception.ConstraintViolationException e) {
            tx.rollback();
            throw new Exception(e.getMessage() + "\nCheck at " + e.getConstraintName());
        } catch (Exception e) {
            tx.rollback();
            throw new Exception(e.getMessage());
        } finally {
            session.close();
        }
    }

    public Object Lay(long id) throws Exception {
        List dssp = null;
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        try {
            org.hibernate.Query query = session.createQuery(hql_lay);
            query.setParameter("id", id);
            dssp = query.list();
        } catch (JDBCConnectionException e) {
            throw new Exception("Lỗi kết nối");
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        } finally {
            session.close();
        }

        if (dssp.isEmpty()) {
            return null;
        }
        return dssp.get(0);
    }

    public void Xoa(Object ob) throws Exception {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        try {
            session.delete(ob);
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            throw new Exception(e.getMessage());
        }
        session.close();
    }
}
